from datetime import datetime

from vnpy_rpcservice import RpcGateway

from trader.gateway.backtester.backtester_tick_gateway import BackTesterTickGateway
from vnpy.event import EventEngine, Event
from vnpy.trader.constant import Exchange
from vnpy.trader.engine import MainEngine
from vnpy.trader.event import EVENT_TICK
from vnpy.trader.object import HistoryRequest, TickData


def process_log_event(event: Event):
    """处理日志事件，将日志信息打印到控制台"""
    tick: TickData = event.data
    print(tick)

def main():
    """
    测试 - rpc调用回测网关
    FIXME 从命令行参数中获取测试参数
    """
    event_engine = EventEngine()
    event_engine.register(EVENT_TICK, process_log_event)

    main_engine = MainEngine(event_engine)
    # main_engine.add_app(CtaStrategyApp)
    rpc_gateway:RpcGateway=main_engine.add_gateway(RpcGateway)

    rpc_gateway.connect({
        "主动请求地址": "tcp://localhost:2014",
        "推送订阅地址": "tcp://localhost:4102"
    })
    main_engine.write_log('RPC gateway started')

    req: HistoryRequest = HistoryRequest(symbol="au2506", exchange=Exchange.SHFE, start=datetime(2024, 8, 1),
                                         end=datetime(2024, 10, 1))
    rpc_gateway.client.query_history(req, BackTesterTickGateway.default_name)
    main_engine.write_log('query_history started')



if __name__ == "__main__":
    main()
